$(document).ready(function(){
	var init_modal_button = '';

	$('button.commend-student').on('click',function(e){
		e.preventDefault();
		init_modal_button = $(this);
	});

	$('button#submit_commendation').on('click',function(e){
		e.preventDefault();
		
		var form = $('form#commend');

		var form_values = form.serialize();
		var process_m = 'Processing please wait.';
		var fail_m = 'Unable to process data.';
		var success_m = 'Process was successfull.';
		var url = $(form).attr('action');
		var eid = init_modal_button.attr('data-url-id');

		$("#msgDialog").dialog('option','height',400);
        $("#msgDialog").dialog('option','buttons','');
		$('.ui-dialog-buttonpane').find('button').addClass('btn btn-mini btn-primary');
		$(".ui-dialog-titlebar-close").hide();
		$( '#msgDialog > p' ).html( 'Are you sure you want to send this message?' );
		$("#msgDialog").dialog("open");

		$("#msgDialog").dialog('option', 'buttons', {
			"Confirm" : function() {
				$(this).dialog("close");

				$('form#commend').block({ 
					css: { 
						border: 'none', 
						padding: '15px',
						backgroundColor: '#000', 
						'-webkit-border-radius': '10px', 
						'-moz-border-radius': '10px', 
						opacity: .5, 
						color: '#fff' 
						},
					message:process_m
				});

				$.ajax({
					'type': 'POST',
					'data': form_values,
					'url' : url+"/"+eid
				}).done(function(result){
					console.log(result);

					if(result == 1){

						$.blockUI.defaults.message = success_m;
						$.blockUI.defaults.css.border ='2px solid #294015';
						$.blockUI.defaults.css.backgroundColor = '#4D7627';
						// alert('asdfasdf');
					}else if (result == 2){
						$.blockUI.defaults.message = success_m;
						$.blockUI.defaults.css.border ='2px solid #155568';
						$.blockUI.defaults.css.backgroundColor = '#2592B1';
					}else if(result == 0){
						$.blockUI.defaults.message = fail_m;
						$.blockUI.defaults.css.border ='2px solid #562221';
						$.blockUI.defaults.css.backgroundColor = '#B94A48';
					}else{
						$.blockUI.defaults.message = fail_m;
						$.blockUI.defaults.css.border ='2px solid #562221';
						$.blockUI.defaults.css.backgroundColor = '#B94A48';
					}
					
					$('form#commend').unblock();
					$.blockUI({
						fadeIn: 700, 
						fadeOut: 700, 
						timeout: 5000, 
						showOverlay: false, 
						centerY: false, 
							css: { 
								width: '350px', 
								top: '48px', 
								left: '', 
								right: '10px', 
								padding: '8px 5px',
								'-webkit-border-radius': '10px', 
								'-moz-border-radius': '10px', 
								opacity: .6, 
								color: '#fff' 
							} 
					}); 

					$('#commendations').foundation('reveal', 'close');

				}).fail(function(){
					$.blockUI.defaults.message = 'Error occured. Unable to update.';
					$.blockUI.defaults.css.border ='2px solid #562221';
					$.blockUI.defaults.css.backgroundColor = '#B94A48';

					$('form#commend').unblock();

					$.blockUI({
						fadeIn: 700, 
						fadeOut: 700, 
						timeout: 5000, 
						showOverlay: false, 
						centerY: false, 
							css: { 
								width: '350px', 
								top: '48px', 
								left: '', 
								right: '10px', 
								padding: '5px',
								'-webkit-border-radius': '10px', 
								'-moz-border-radius': '10px', 
								opacity: .6, 
								color: '#fff' 
							} 
					}); 
				});
			},
			"Cancel" : function() {
				$(this).dialog("close");
			}
		});
	});
});